home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 4.2 KB | 164 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: SLCyUtil.cpp
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- // Cyberdog Utilities
- //
- //========================================================================================
-
- #ifndef FWFRAMEW_HPP
- #include "FWFrameW.hpp"
- #endif
-
- #ifndef SLCYUTIL_H
- #include "SLCyUtil.h"
- #endif
-
- #ifndef FWMEMMGR_H
- #include "FWMemMgr.h"
- #endif
-
- #ifndef SOM_FW_OSink_xh
- #include "SLASinks.xh"
- #endif
-
- #ifndef FWSTREAM_H
- #include "FWStream.h"
- #endif
-
- //
- // Cyberdog
- //
-
- #ifndef __CYBERDOG__
- #include <Cyberdog.h>
- #endif
-
- #ifndef SOM_CyberItem_xh
- #include <CyberItem.xh>
- #endif
-
- #ifndef SOM_CyberSession_xh
- #include <CyberSession.xh>
- #endif
-
- //========================================================================================
- // Class FW_CAcquiredMemory
- //========================================================================================
- #pragma mark Class FW_CAcquiredMemory
-
- class FW_CAcquiredMemory {
- public:
- FW_DECLARE_AUTO (FW_CAcquiredMemory)
- FW_CAcquiredMemory (void* memory) :fMemory(memory){}
- ~FW_CAcquiredMemory ();
- void* operator & () { return fMemory; }
- private:
- void* fMemory;
- };
-
- //-----------------------------------------------------------------------------
- // FW_CAcquiredMemory Runtime Information
- //-----------------------------------------------------------------------------
-
- FW_DEFINE_AUTO (FW_CAcquiredMemory)
-
- //-----------------------------------------------------------------------------
- // FW_CAcquiredMemory::~FW_CAcquiredMemory
- //-----------------------------------------------------------------------------
-
- FW_CAcquiredMemory::~FW_CAcquiredMemory ()
- {
- if (fMemory)
- FW_CMemoryManager::FreeBlock (fMemory);
- }
-
- #pragma mark -
- //-----------------------------------------------------------------------------
- // FW_CyberdogIsInstalled
- //-----------------------------------------------------------------------------
-
- FW_Boolean FW_CyberdogIsInstalled (Environment* ev)
- {
- FW_UNUSED (ev);
-
- return (&GetCyberSession != 0);
- }
-
- /*
- Utilities for getting CyberItems into and out of ODF streams.
-
- Format of a CyberItem:
-
- long length (includes this field)
- short signature ('cy')
- short version
- long classid length
- N classid
- <private data>
- */
-
- //-----------------------------------------------------------------------------
- // FW_WriteCyberItem
- //-----------------------------------------------------------------------------
-
- extern "C"
- void FW_WriteCyberItem (Environment* ev, CyberItem* ci, FW_OSink* sink)
- {
- long size = ci->GetFlatSize(ev);
- FW_CAcquiredMemory tbuffer = FW_CMemoryManager::AllocateBlock (size);
- char* buffer = (char*) &tbuffer;
- size = ci->Flatten (ev, buffer, size);
- sink->Write (ev, buffer, size);
- }
-
- //-----------------------------------------------------------------------------
- // FW_ReadCyberItem
- //-----------------------------------------------------------------------------
-
- extern "C"
- CyberItem* FW_ReadCyberItem (Environment* ev, CyberSession* session, FW_OSink* sink)
- {
- // First we read in the classname to create the CyberItem
- FW_CReadableStream header (sink);
- long size, classsize;
- short signature, version;
- header >> size >> signature >> version >> classsize;
- if (signature != 'cy')
- FW_Failure (FW_xCorruptArchiveStream);
-
- FW_CAcquiredMemory tclassname = FW_CMemoryManager::AllocateBlock (classsize+1);
- char* classname = (char*) &tclassname;
- header.Read (classname, classsize);
- classname[classsize] = 0;
-
- // Create a buffer with the CyberItem header and data
- FW_CAcquiredMemory tbuffer = FW_CMemoryManager::AllocateBlock (size);
- char* buffer = (char*) &tbuffer;
- FW_PMemorySink copysink (ev, buffer, size, 0);
- FW_CWritableStream copy (copysink);
- copy << size << signature << version << classsize;
- copy.Write (classname, classsize);
- long offset = copysink->GetPosition(ev);
- sink->Read (ev, buffer + offset, size - offset);
-
- // Finally create the CyberItem and read it from the memory buffer.
- CyberItem* ci = session->NewCyberItem (ev, classname);
- FW_TRY {
- ci->ICyberItem (ev);
- ci->Unflatten (ev, buffer);
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING() {
- delete ci;
- FW_THROW_SAME();
- }
- FW_CATCH_END
-
- return ci;
- }
-
-